package org.jeecg.modules.xl.mpapi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.annotation.Export2Swagger;
import org.jeecg.modules.xl.mpapi.vo.ShopCartAddModel;
import org.jeecg.modules.xl.mpapi.vo.ShopCartVo;
import org.jeecg.modules.xl.shengfutong.response.PreUnifieAppletdorderPayInfo;
import org.jeecg.modules.xl.shopcart.entity.XlShopcart;
import org.jeecg.modules.xl.shopcart.service.IXlShopcartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author deco
 * @create 2025-02-24
 */
@Api(tags="小程序-购物车接口")
@RestController
@RequestMapping("/htmmp/shopcart")
@Export2Swagger
@Slf4j
public class ShopcartController {

    @Autowired
    private IXlShopcartService shopcartService;

    @ApiOperation(value = "添加购物车", notes = "添加购物车")
    @PostMapping(value = "/addShopcart")
    @Export2Swagger
    public Result<JSONObject> addShopcart(@RequestBody ShopCartAddModel shopcart) {
        Result<JSONObject> result = new Result<JSONObject>();
        XlShopcart  xlShopcart = new XlShopcart();
        BeanUtils.copyProperties(shopcart, xlShopcart);
        return shopcartService.addShopcart(xlShopcart);
    }

    @ApiOperation(value = "删除购物车产品", notes = "删除购物车产品")
    @PostMapping(value = "/removeShopcartItem")
    @Export2Swagger
    public Result<JSONObject> removeShopcartItem(@RequestBody ShopCartAddModel shopcart) {
        Result<JSONObject> result = new Result<JSONObject>();

        XlShopcart  xlShopcart = new XlShopcart();
        BeanUtils.copyProperties(shopcart, xlShopcart);

        return shopcartService.removeShopcartItem(xlShopcart);
    }

    @ApiOperation(value = "清空购物车", notes = "清空购物车")
    @GetMapping(value = "/clearCart")
    @Export2Swagger
    public Result<JSONObject> clearCart(  @RequestParam(value = "storeCode") String storeCode ){
        return shopcartService.clearCart(storeCode);
    }
    @ApiOperation(value = "查询购物车", notes = "查询购物车")
    @GetMapping(value = "/getCart")
    @Export2Swagger
    public Result<List<ShopCartVo>> getCart(@RequestParam(value = "storeCode") String storeCode ){
        return Result.OK(shopcartService.getCart(storeCode));
    }

    @ApiOperation(value = "提交订单", notes = "提交订单")
    @GetMapping(value = "/submitOrder")
    @Export2Swagger
    public Result<Object> submitOrder(@RequestParam(value = "storeCode") String storeCode, @RequestParam(value = "orderNote") String orderNote ){
        return shopcartService.submitOrder(storeCode,orderNote);
    }

    @ApiOperation(value = "盛付通半屏小程序支付接口", notes = "盛付通半屏小程序支付接口")
    @GetMapping(value = "/preUnifieAppletdorder")
    @Export2Swagger
    public Result<PreUnifieAppletdorderPayInfo> preUnifieAppletdorder(@RequestParam(value = "orderCode") String orderCode ) throws Exception {

        PreUnifieAppletdorderPayInfo preUnifieAppletdorderPayInfo=  shopcartService.preUnifieAppletdorder(orderCode);
        if(ObjectUtil.isEmpty(preUnifieAppletdorderPayInfo)){
            return Result.error("发起支付异常，请重试");
        }
        return Result.ok(preUnifieAppletdorderPayInfo);
    }
}